package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.constant.CommonConstant;
import com.lam.common.utils.StringUtils;
import com.lam.mall.entity.ProdSku;
import com.lam.mall.mapper.ProdSkuMapper;
import com.lam.mall.param.ProdSkuParam;
import com.lam.mall.service.ProdSkuService;

/**
 * 商品skuService业务层处理
 * 
 * @author admin
 * @date 2023-04-22
 */
@Service
public class ProdSkuServiceImpl  implements ProdSkuService {

    @Autowired
    private ProdSkuMapper prodSkuMapper;

    @Override
	@Cacheable(cacheNames = "skuList", key = "#prodId")
	public List<ProdSku> listByProdIdCache(String prodId) {
		return listByProdId(prodId);
	}

	@Override
	@Cacheable(cacheNames = "sku", key = "#skuId")
	public ProdSku getBySkuIdCache(String skuId) {
		return prodSkuMapper.selectById(skuId);
	}

	@Override
	@Caching(evict = {
			@CacheEvict(cacheNames = "sku", key = "#skuId"),
			@CacheEvict(cacheNames = "skuList", key = "#prodId")
	})
	public void removeCacheBySkuId(String skuId, String prodId) {
	}
	
    /**
     * 查询商品sku
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public ProdSku selectById(String id) {

        return prodSkuMapper.selectById(id);
    }
    
    @Override
	public List<ProdSku> listByProdId(String prodId) {
    	QueryWrapper<ProdSku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("prod_id", prodId);
        queryWrapper.eq("del_status", CommonConstant.DEL_STATUS_NORMAL);
        return prodSkuMapper.selectList(queryWrapper);
	}

    /**
     * 查询商品sku列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<ProdSku> selectList(ProdSkuParam param) {
    
        QueryWrapper<ProdSku> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("prod_name", param.getKeywords());
                wr.or().like("sku_name", param.getKeywords());
            });
        }
        
        queryWrapper.eq(Objects.nonNull(param.getProdId()), "prod_id", param.getProdId());
        queryWrapper.like(StringUtils.isNotBlank(param.getProdNameKey()), "prod_name", param.getProdNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getProdName()), "prod_name", param.getProdName());
        queryWrapper.like(StringUtils.isNotBlank(param.getSkuNameKey()), "sku_name", param.getSkuNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getSkuName()), "sku_name", param.getSkuName());
        queryWrapper.eq(StringUtils.isNotBlank(param.getModelId()), "model_id", param.getModelId());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.eq(Objects.nonNull(param.getDelStatus()), "del_status", param.getDelStatus());

        return prodSkuMapper.selectList(queryWrapper);
    }

    /**
     * 新增商品sku
     * 
     * @param prodSku 
     * @return 
     */
    @Override
    @Transactional
    public int insert(ProdSku prodSku) {
    
        return prodSkuMapper.insert(prodSku);
    }

    /**
     * 修改商品sku
     * 
     * @param prodSku 
     * @return 
     */
    @Override
    @Transactional
    public int update(ProdSku prodSku) {
    
        return prodSkuMapper.updateById(prodSku);
    }

    /**
     * 删除商品sku信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return prodSkuMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    @Cacheable(cacheNames = "skuList", key = "#prodId")
    @Override
	public int deleteByProdId(String prodId) {
		
		return prodSkuMapper.deleteByProdId(prodId);
		
	}

    @Cacheable(cacheNames = "skuList", key = "#prodId")
	@Override
	public int logicDeleteByProdId(String prodId) {
		
		return prodSkuMapper.logicDeleteByProdId(prodId);
	}
	
}
